#include "Plugin.h"
#include "PluginLoader.h"
#include <debugx>
#include "processwin.h"

#include "MainWinUtil.h"
#include "QMainWindow"
#include "QMenuBar"
#include "QFile"


QDockWidget *dockOpencv=0;

extern QDockWidget *dockProcess;
extern ProcessWin *processWin;
//ImageProcess

QDockWidget * dockFileBrowse=0;
QDockWidget * dockFolder=0;
QDockWidget * dockFileInfo=0;
Plugin *plugin=0;

typedef void *(*voidPointFun_t)();
QDockWidget * getDock(QString funName)
{
    auto pluginLoader=plugin->pluginLoader;
    auto info= pluginLoader->loadPluginByFileName("FileBrowse.dll");
    if(!info)return 0;
    info->plugin->init(pluginLoader);
    auto fun= (voidPointFun_t)(info-> plugin->functionMap.value(funName));
    return (QDockWidget *)(fun());

}
void getDockFileBrowse()
{
    if(dockFileBrowse)return;
     dockFileBrowse=  getDock("getDockFileBrowse");
}

void getDockFolder()
{
    if(dockFolder)return;
    dockFolder= getDock("getDockFolder");
    getDockFileBrowse();

    auto mainWin=static_cast<QMainWindow *>(plugin->getMainWin()) ;
    mainWin->splitDockWidget(dockFolder,dockFileBrowse,Qt::Vertical);
    mainWin->resizeDocks({dockFolder,dockFileBrowse},{1,3},Qt::Vertical);


}


void showImageDft(QImage &img);
std::string filePathForOpencv;
bool isZip=false;
void showImage(QString filePath) {

    filePathForOpencv=filePath.toLocal8Bit();
    void showImageProcess();
    showImageProcess();
}





void actionProcess(bool )
{

    hideAllDock(plugin->getMainWin());
    auto mainWin=static_cast<QMainWindow *>(plugin->getMainWin()) ;

    getDockFolder();
    void getDockProcess();
    getDockProcess();
    dockFileBrowse->show();
    dockFolder->show();
    dockProcess->show();

    mainWin->splitDockWidget(dockFolder,dockProcess,Qt::Horizontal);
    mainWin->splitDockWidget(dockFolder,dockFileBrowse,Qt::Vertical);
    mainWin->resizeDocks({dockFolder,dockFileBrowse},{1,3},Qt::Vertical);
}

void Plugin::loadWidget()
{
    plugin=this;
    unloadable=true;


    void initProcessFunMap();
    initProcessFunMap();
}

void Plugin::exposeAction()
{

    actionMap.insert("QtSample",actionProcess);

}

void Plugin::exposeFunction()
{



}

void Plugin::initMainWin()
{

}
int Plugin::slot_msg(int type,void *userdata)
{

   return processMsg(type,userdata);
}
int Plugin::processMsg(int type, void *userdata) {
    debugx2(type);
   if (type == ImageFilePath ||type == ImageFilePathZip)
   {
        isZip=(type == ImageFilePathZip);
        showImage(*(QString *)userdata);
        return 0;
   }
   if (type == RePaint)
   {

        if(processWin && processWin->isVisible())
            processWin->processMsg(userdata);

        return 0;
   }

    return 0;
}


bool Plugin::unload()
{
    return true;
}
